package com.cyf.db.mapper;

import com.cyf.db.bean.VisitorRecord;
import org.apache.ibatis.annotations.*;

import java.util.Date;
import java.util.List;

/**
 * 访客记录
 * Created by cage on 2017/9/18.
 */

public interface VisitorRecordMapper {



    @Insert("insert into visitor_record(intervieweeId,visitorId,createdTime," +
            "estimateVisitorTime,endTime,status,mobile,name,memo,plate, orderNo, apiUserId) " +
            "values(#{intervieweeId}, #{visitorId}, #{createdTime}, " +
            "#{estimateVisitorTime}, #{endTime}, #{status}, #{mobile}," +
            "#{name},#{memo},#{plate}, #{orderNo}, #{apiUserId})")
    @SelectKey(before = false, resultType = int.class, statement = "select LAST_INSERT_ID()", keyProperty = "id")
    void insertVisitorRecord(VisitorRecord visitorRecord);

    // or status = 1
    @Select("select * from visitor_record where visitorId = #{visitorId} and mobile = #{mobile} and (status = 0 or status = 1)")
    VisitorRecord getByVisitorIdAndMobile(@Param("visitorId") Integer visitorId, @Param("mobile") String mobile);

    @Update("update visitor_record set status = #{status} where id = #{id}")
    void updateStatusById(@Param("status") Integer status, @Param("id") Integer id);

    @Select("SELECT * from visitor_record where id = #{id}")
    VisitorRecord getById(@Param("id") Integer id);

    @Select("select id from visitor_record where endTime <= #{now} and (status = 0 or status = 1)")
    List<Integer> timeout(@Param("now") Date now);

    @Select("select * from visitor_record where visitorId = #{visitorId} order by estimateVisitorTime desc limit ${start}, ${end}")
    List<VisitorRecord> getByVisitorId(@Param("visitorId") Integer visitorId, @Param("start") Integer start, @Param("end") Integer end);

    @Select("select count(id) from visitor_record where visitorId = #{visitorId}")
    int getCountByVisitorId(@Param("visitorId") Integer visitorId);

    @Select("select * from visitor_record where mobile = #{mobile} and status = #{status}")
    List<VisitorRecord> getByMobileAndStatus(@Param("mobile") String mobile, @Param("status") Integer status);

    @Update("update visitor_record set intervieweeId = #{intervieweeId} where mobile = #{mobile}")
    void updateIntervieweeIdByMobile(@Param("intervieweeId") Integer intervieweeId, @Param("mobile") String mobile);

    @Update("update visitor_record set status = #{status}, rejectMemo = #{rejectMemo}, endTime = #{endTime} where id = #{id}")
    void rejectVisit(VisitorRecord visitorRecord);
}
